#include "UI_Menu_Index.h"
#include "GUI_core.h"
#include "GUI_Touch.h"
#include "GUI_Image.h"
#include "Time.h"
//显示缓冲区
extern uint8_t displaybuf1[LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT]; //主缓存区
extern uint8_t displaybuf2[LCD_GUI_WIDTH*2*LCD_REFRESH_UNIT]; //备份缓存区

extern uint8_t LCD_Display_Buf[LCD_GUI_HEIGHT][LCD_GUI_WIDTH*2];

extern Touch_Slider_t  Touch_Slider;

Move_Dir_t move_dir;

uint16_t Xoffset=0;
uint16_t yoffset=0;

uint16_t Scroll_Flag=0;


extern Time_t my_Time;
































Dialplate_Point_t dialplate_01_Display=
{
	.Point_Hour={

		time06_H_01Image_ADDRESS,time06_H_02Image_ADDRESS,time06_H_03Image_ADDRESS,time06_H_04Image_ADDRESS,time06_H_05Image_ADDRESS,time06_H_06Image_ADDRESS,time06_H_07Image_ADDRESS,time06_H_08Image_ADDRESS,
		time06_H_09Image_ADDRESS,time06_H_10Image_ADDRESS,time06_H_11Image_ADDRESS,time06_H_12Image_ADDRESS,time06_H_13Image_ADDRESS,time06_H_14Image_ADDRESS,time06_H_15Image_ADDRESS,time06_H_16Image_ADDRESS
	},
	.Point_Minute={
		time06_M_01Image_ADDRESS,time06_M_02Image_ADDRESS,time06_M_03Image_ADDRESS,time06_M_04Image_ADDRESS,time06_M_05Image_ADDRESS,time06_M_06Image_ADDRESS,time06_M_07Image_ADDRESS,time06_M_08Image_ADDRESS,
        time06_M_09Image_ADDRESS,time06_M_10Image_ADDRESS,time06_M_11Image_ADDRESS,time06_M_12Image_ADDRESS,time06_M_13Image_ADDRESS,time06_M_14Image_ADDRESS,time06_M_15Image_ADDRESS,time06_M_16Image_ADDRESS

	},
	.Point_Second={
		time06_S_01Image_ADDRESS,time06_S_02Image_ADDRESS,time06_S_03Image_ADDRESS,time06_S_04Image_ADDRESS,time06_S_05Image_ADDRESS,time06_S_06Image_ADDRESS,time06_S_07Image_ADDRESS,time06_S_08Image_ADDRESS,
		time06_S_09Image_ADDRESS,time06_S_10Image_ADDRESS,time06_S_11Image_ADDRESS,time06_S_12Image_ADDRESS,time06_S_13Image_ADDRESS,time06_S_14Image_ADDRESS,time06_S_15Image_ADDRESS,time06_S_16Image_ADDRESS
	},
};









Current_Menu_t Current_menu=
{
  Time_Menu,  //当前界面
  Sport_Menu, //上一界面
  Heart_Menu, //下一界面
};


Menu_t Menu_Init[]=
{

	{Time_Menu,   	{Time_Menu_Display,Time_Menu_Touch}	        },  //时间界面
	{Heart_Menu,    {Heart_Menu_Display,Heart_Menu_Touch}    	},  //心率界面
	{Sport_Menu,   	{Sport_Menu_Display,Sport_Menu_Touch}   	},  //运动界面
	{Setting_Menu,	{Setting_Menu_Display,Setting_Menu_Touch}  	},  //设置界面
	{Power_Menu,  	{Power_Menu_Display,Power_Menu_Touch}  		},  //关机界面
}; 

Menu_Func_t * Find_Current_Menu(Menu_Index index)
{
	for(int i=0;i<(sizeof(Menu_Init)/sizeof(Menu_Init[0]));i++)
	{
	
		if(Menu_Init[i].index==index)
		{
			Current_menu.current_menu=index;
	        return  &Menu_Init[i].menu_function;
		}
	
	}
    return NULL;

}


void Menu_Display_Handler_Process(Menu_Index index)
{
	if(	( (Menu_Func_t *) Find_Current_Menu(index))->Display  !=NULL)
	{
		( (Menu_Func_t *) Find_Current_Menu(index))->Display();  //执行对应的显示函数
	}

}


void Menu_Touch_Handler_Process(Menu_Index index)
{
	if(	( (Menu_Func_t *) Find_Current_Menu(index))->Touch  !=NULL)
	{
		( (Menu_Func_t *) Find_Current_Menu(index))->Touch();//执行对应的触摸函数
	}
}

int  flag=0;
static int count=0;
static int second_count=0;
static int minute_count=0;
static int hour_count=0;
static int scroll_num=20;

uint8_t buf_temp[240*2*40]={0};

uint8_t address_flag=0;

#include<stdio.h>
#include<stdlib.h>
static void LCD_Reflesh_Buf(uint8_t *buffer,uint8_t *buffer1,uint8_t ypos)
{

	memcpy(LCD_Display_Buf,displaybuf2,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

	for(int i=0;i<LCD_GUI_HEIGHT-ypos;i++)
	{
	memcpy(&LCD_Display_Buf[i],&LCD_Display_Buf[i+ypos],LCD_GUI_WIDTH*2); //将显示缓冲区2更新到主缓冲区1
	}
	//memcpy(buffer,buffer[ypos],);

//填充要显示的区域
//for(int i=LCD_GUI_HEIGHT-ypos;i<LCD_GUI_HEIGHT;i++)
{
	memcpy(&LCD_Display_Buf[LCD_GUI_HEIGHT-ypos],buffer1,LCD_GUI_WIDTH*2*ypos); //将显示缓冲区2更新到主缓冲区1
}

memcpy(displaybuf2,LCD_Display_Buf,LCD_GUI_WIDTH*2*LCD_GUI_HEIGHT); //将显示缓冲区2更新到主缓冲区1

}



uint32_t   second_time[]=
{
  main_1_S1Image_ADDRESS,
  main_1_S2Image_ADDRESS,
  main_1_S3Image_ADDRESS,
  main_1_S4Image_ADDRESS,
  main_1_S5Image_ADDRESS,
  main_1_S6Image_ADDRESS,
  main_1_S7Image_ADDRESS,
  main_1_S8Image_ADDRESS,

};

uint32_t   minute_time[]=
{
  main_1_M1Image_ADDRESS ,
  main_1_M2Image_ADDRESS ,
  main_1_M3Image_ADDRESS ,
  main_1_M4Image_ADDRESS ,
  main_1_M5Image_ADDRESS ,
  main_1_M6Image_ADDRESS ,
  main_1_M7Image_ADDRESS ,
  main_1_M8Image_ADDRESS ,

};

uint32_t   hour_time[]=
{
  main_1_H1Image_ADDRESS  ,
  main_1_H2Image_ADDRESS  ,
  main_1_H3Image_ADDRESS  ,
  main_1_H4Image_ADDRESS  ,
  main_1_H5Image_ADDRESS  ,
  main_1_H6Image_ADDRESS  ,
  main_1_H7Image_ADDRESS  ,
  main_1_H8Image_ADDRESS  ,

};




void Time_Menu_Display()//时间界面
{

static   int second=0,minute=0,hour=0;
static int pos=0,offset=1;
uint8_t buf[10]={0};


if(flag==0)
{
	//lcm_set_windows(0,0,240,280);


	LCD_Set_Area_Display(20, 280+20);	//setting display area



	GUI_Write_LCD_Display_Buf(0,0,0,0,bg4Image_ADDRESS,displaybuf2);
	//st7796h_draw(0, 0, LCD_GUI_WIDTH,LCD_GUI_HEIGHT, displaybuf2);   //refresh LCD buf




	 GUI_Show_Image_Alpha(10,20,_1Image_ADDRESS);
	 GUI_Show_Image_Alpha(25,20,_2Image_ADDRESS);
	 GUI_Show_Image_Alpha(40,20,aImage_ADDRESS);
	 GUI_Show_Image_Alpha(55,20,bImage_ADDRESS);
	 GUI_Show_Image_Alpha(10,40,w1Image_ADDRESS);

	 GUI_Show_Image_Alpha(0,0,hour_time[hour]/*main_1_S1Image_ADDRESS*/);
	
	 GUI_Show_Image_Alpha(0,0,minute_time[minute]/*main_1_S1Image_ADDRESS*/);


	 GUI_Show_Image_Alpha(0,0,second_time[second]/*main_1_S1Image_ADDRESS*/);


	st7796h_draw(0, 0, LCD_GUI_WIDTH,LCD_GUI_HEIGHT, displaybuf2);	 //refresh LCD buf

	second++;

	if(second>=8)
	{
		second=0;
		minute++;
		
	}


    //GUI_Write_LCD_Display_Buf_Part(0,0,0,0,cat4Image_ADDRESS,buf_temp,20);

	//st7796h_draw(0,280,240,20,buf_temp);



	//LCD_Set_Area_Display(1, 300);   //setting display area
	//lcm_set_windows(0,0,240,280);



	
	flag=0;
}else
{





#if 0    //软件控制显存刷屏滚动

	 GUI_Write_LCD_Display_Buf_Part(0,0,0,0,cat4Image_ADDRESS/*+(address_flag*1)*240*2*/,buf_temp,10,address_flag);	
	 LCD_Reflesh_Buf(buf,buf_temp,10);	
	 st7796h_draw(0, 0, LCD_GUI_WIDTH,LCD_GUI_HEIGHT, displaybuf2);   //refresh LCD buf
	 printf("address_flag=%d\r\n",address_flag);
	
	
	 //scroll_num=30;
	 scroll_num+=10;
	
	address_flag++;
	
	
	 
	// LCD_Set_Area_Display(320-scroll_num, 320);	//setting display area
	
	if(address_flag>=28)
		 {
			 //scroll_num=20;
			 //flag=0;
			 address_flag=0;
		 }
	
	
	
	



#endif








	scroll_num=20;
	LCD_Vertical_Scroll_Mode_Display(); 	  //setting scroll mode

	//LCD_Vertical_Scroll_Mode_Display();       //setting scroll mode

#if 0
	while(1)
{
	LCD_Set_Address(scroll_num);
	
  if(scroll_num==30)
  	{
	    LCD_Vertical_Scroll_Mode_Exit();//exit scroll mode
		LCD_Write_Cmd(0x13);
		//LCD_Write_Cmd(0x29);
	
		GUI_Write_LCD_Display_Buf_Part(0,0,0,0,cat4Image_ADDRESS/*+(address_flag*1)*240*2*/,buf_temp,10,address_flag);
	   
		st7796h_draw_Offset(0,280/*address_flag*10*/,240,10,buf_temp);
	
		LCD_Set_Area_Display(20, 280+20);	//setting display area
		break;
  	}else if(scroll_num==40)
  	{


	}
	scroll_num+=10;

	 
}	


#endif

#if 0

	LCD_Vertical_Scroll_Mode_Exit();//exit scroll mode
	LCD_Write_Cmd(0x13);
	//LCD_Write_Cmd(0x29);

    GUI_Write_LCD_Display_Buf_Part(0,0,0,0,cat4Image_ADDRESS/*+(address_flag*1)*240*2*/,buf_temp,10,address_flag);
   
    st7796h_draw_Offset(0,280/*address_flag*10*/,240,10,buf_temp);

	LCD_Set_Area_Display(20, 280+20);	//setting display area

#endif

    printf("address_flag=%d\r\n",address_flag);


	//scroll_num=30;
    scroll_num+=10;

   address_flag++;


	
   // LCD_Set_Area_Display(320-scroll_num, 320);   //setting display area

   if(address_flag>=28)
		{
			//scroll_num=20;
			//flag=0;
			address_flag=0;
		}






	//pos=+offset;

	//LCD_Vertical_Scroll_Mode_Exit();

	
	//st7796h_draw(0, 280-scroll_num, LCD_GUI_WIDTH, 1 , displaybuf2);	 //refresh LCD buf


    //GUI_Write_LCD_Display_Buf(0,0,0,0,bg1Image_ADDRESS,displaybuf2);

#if 1
   if(scroll_num>=280+10)
		{
			scroll_num=20;
			flag=2;
			address_flag=0;
		}
#endif

}

#if 0
	GUI_Write_LCD_Display_Buf(0,0,0,0,bg1Image_ADDRESS,displaybuf2);


	//GUI_Show_Dialplate_Pointer_Time(LCD_GUI_WIDTH/2,LCD_GUI_HEIGHT/2,dialplate_01_Display.Point_Hour,    7,my_Time.hour);
	//GUI_Show_Dialplate_Pointer_Time(LCD_GUI_WIDTH/2,LCD_GUI_HEIGHT/2,dialplate_01_Display.Point_Minute,7,my_Time.minute);
	GUI_Show_Dialplate_Pointer_Time(LCD_GUI_WIDTH/2,LCD_GUI_HEIGHT/2,&dialplate_01_Display.Point_Second,17,my_Time.seconds);

	//GUI_Show_Dialplate_Pointer_Time(LCD_GUI_WIDTH/2,LCD_GUI_HEIGHT/2,&dialplate_01_Display.Point_Second,17,15);



#endif

#if 0    //显示指针0-15
	GUI_Write_LCD_Display_Buf(0,0,0,0,bg1Image_ADDRESS,displaybuf2);
    GUI_Show_Dialplate_Point(LCD_GUI_WIDTH/2,LCD_GUI_HEIGHT/2,dialplate_01_Display.Point_Hour[hour_count].Address,7);
	GUI_Show_Dialplate_Point(LCD_GUI_WIDTH/2,LCD_GUI_HEIGHT/2,dialplate_01_Display.Point_Minute[minute_count].Address,7);
	GUI_Show_Dialplate_Point(LCD_GUI_WIDTH/2,LCD_GUI_HEIGHT/2,dialplate_01_Display.Point_Second[second_count].Address,17);
	if(count>=10)
	{
		count=0;


		second_count++;
		if(second_count>=15)
		{
			second_count=0;
			minute_count++;
			if(minute_count>=15)
			{
				minute_count=0;
				hour_count++;
				if(hour_count>=15)
				{

					hour_count=0;
				}

			}



		}

	}
	count++;

#endif
	
#if  0
	uint8_t i=0;
	  //printf("%s\r\n",__FUNCTION__);
	

		  GUI_Write_LCD_Display_Buf(0,0,0,0,bg1Image_ADDRESS,displaybuf2);
		
		printf("minute=%d,seconds=%d\r\n",my_Time.minute,my_Time.seconds);
	
		  //GUI_Show_Image_Num(40+0*33,20,num47Image_ADDRESS,0);
		  GUI_Show_Image_Num_Offset(120,20+   Touch_Slider.offset/*flag*/,num47Image_ADDRESS,my_Time.seconds/10);
		  GUI_Show_Image_Num_Offset(120+33,20+Touch_Slider.offset/*flag*/,num47Image_ADDRESS,my_Time.seconds%10);


		  
		  //GUI_Show_Image_Num(0+0*33,flag,num47Image_ADDRESS,my_Time.hour/10);
		  //GUI_Show_Image_Num(0+1*33,flag,num47Image_ADDRESS,my_Time.hour%10);

	flag+=5;

	  

#if 0        //显示当前的时间
	if(flag==0)
	{
	  GUI_Write_LCD_Display_Buf(0,0,0,0,bg1Image_ADDRESS,displaybuf2);
	
	printf("minute=%d,seconds=%d\r\n",my_Time.minute,my_Time.seconds);

	  //GUI_Show_Image_Num(40+0*33,20,num47Image_ADDRESS,0);

	  GUI_Show_Image_Num(0+0*33,100,num47Image_ADDRESS,my_Time.hour/10);
	  GUI_Show_Image_Num(0+1*33,100,num47Image_ADDRESS,my_Time.hour%10);

	  
	  GUI_Show_Image_Num(50+1*33,100,num47Image_ADDRESS,my_Time.minute/10);
	  GUI_Show_Image_Num(50+2*33,100,num47Image_ADDRESS,my_Time.minute%10);
	  GUI_Show_Image_Num(60+3*33,100,num47Image_ADDRESS,my_Time.seconds/10);
	  GUI_Show_Image_Num(60+4*33,100,num47Image_ADDRESS,my_Time.seconds%10);
  }
    flag=1;
#endif

/*
	  GUI_Show_Image_Num(40+0*33,20+50,num47Image_ADDRESS,5);
	  GUI_Show_Image_Num(40+1*33,20+50,num47Image_ADDRESS,6);
	  GUI_Show_Image_Num(40+2*33,20+50,num47Image_ADDRESS,7);
	  GUI_Show_Image_Num(40+3*33,20+50,num47Image_ADDRESS,8);
	  GUI_Show_Image_Num(40+4*33,20+50,num47Image_ADDRESS,9);
*/


#if 0   //显示星期
	  GUI_Show_Image_Week(40+0*37,100+50,weekImage_ADDRESS,1);
	  GUI_Show_Image_Week(40+1*37,100+50,weekImage_ADDRESS,2);
	  GUI_Show_Image_Week(40+2*37,100+50,weekImage_ADDRESS,3);
	  GUI_Show_Image_Week(40+3*37,100+50,weekImage_ADDRESS,4);

	  
	  GUI_Show_Image_Week(40+0*37,150+50,weekImage_ADDRESS,5);
	  GUI_Show_Image_Week(40+1*37,150+50,weekImage_ADDRESS,6);
	  GUI_Show_Image_Week(40+2*37,150+50,weekImage_ADDRESS,7);
#endif

	  
#endif

#if 0

	  //flag+=5;
	}//else
	{

    #if 0
	
	if(	Touch_is_pressed())
		if(Scroll_Flag)
		{
			move_dir=Move_Up;
			#if 0
			GUI_Horizon_Scroll(Xoffset,move_dir);
			#endif

			LCD_Vertical_Scroll_Display(move_dir,yoffset);
			printf("Up Scroll yoffset=%d\r\n",yoffset);
		}
		#else

#if 0
	if(Touch_Slider.status==TOUCH_Down)
	{
		move_dir=Move_Down;
		//for(i=0;i<Touch_Slider.offset;i++)
		LCD_Vertical_Scroll_Display(move_dir,20);
		Touch_Slider.status=TOUCH_Start;
		Touch_Slider.offset=0;


	}else if(Touch_Slider.status==TOUCH_Up)
	{
		move_dir=Move_Up;
		//for(i=0;i<Touch_Slider.offset;i++)
		LCD_Vertical_Scroll_Display(move_dir,20);
		//LCD_Vertical_Scroll_Display(move_dir,Touch_Slider.offset);
		Touch_Slider.status=TOUCH_Start;
		Touch_Slider.offset=0;
	}

#endif




#if 0
	    LCD_Vertical_Scroll_Display(move_dir,5);
#endif
		//yoffset+=8;
	   //printf("Up Scroll yoffset=%d\r\n",yoffset);

		#endif
	}

	 //LCD_Draw_Image(0,0,0,0,bg2Image_ADDRESS);



#endif

}
void Heart_Menu_Display()     //心率界面
{
	  //printf("%s\r\n",__FUNCTION__);
	 GUI_Write_LCD_Display_Buf(0,0,0,0,bg3Image_ADDRESS,displaybuf2);
	 //LCD_Draw_Image(0,0,0,0,bg3Image_ADDRESS);

}
void Sport_Menu_Display()     //运动界面
{
    GUI_Write_LCD_Display_Buf(0,0,0,0,bg2Image_ADDRESS,displaybuf2);

  printf("%s\r\n",__FUNCTION__);
}
void Setting_Menu_Display()   //设置界面
{
  printf("%s\r\n",__FUNCTION__);
}
void Power_Menu_Display()//关机界面
{
  printf("%s\r\n",__FUNCTION__);
}




void Time_Menu_Touch()
{

	if(Touch_Slider.status==TOUCH_Down)
	{
	   Current_menu.current_menu=Heart_Menu;

	
	#if 0
	printf("%s  TOUCH_Down\r\n",__FUNCTION__);
		move_dir=Move_Down;
		//for(i=0;i<Touch_Slider.offset;i++)
		LCD_Vertical_Scroll_Display(move_dir,20);
		Touch_Slider.status=TOUCH_Start;
		Touch_Slider.offset=0;
		#endif

	}else if(Touch_Slider.status==TOUCH_Up)
	{
	
		Current_menu.current_menu=Sport_Menu;
	#if 0
		printf("%s	TOUCH_Up\r\n",__FUNCTION__);

		move_dir=Move_Up;
		//for(i=0;i<Touch_Slider.offset;i++)
		LCD_Vertical_Scroll_Display(move_dir,20);
		//LCD_Vertical_Scroll_Display(move_dir,Touch_Slider.offset);
		Touch_Slider.status=TOUCH_Start;
		Touch_Slider.offset=0;

	#endif
	}




#if 0
if(	Touch_is_pressed())

	{
		Scroll_Flag=1;
		//printf("touch press\r\n");
		//yoffset+=1;
		if(yoffset>=240)
		{
			yoffset=0;
			flag=0;
		}

	}else
	{
	    yoffset+=1;
		//printf("touch release\r\n");
		Scroll_Flag=0;
	}
	 //Current_menu.current_menu=Heart_Menu;
	 //printf("%s\r\n",__FUNCTION__);
#else
#if 0

if(Touch_Slider.status==TOUCH_Down)
{
printf("%s  TOUCH_Down\r\n",__FUNCTION__);
	move_dir=Move_Down;
	//for(i=0;i<Touch_Slider.offset;i++)
	LCD_Vertical_Scroll_Display(move_dir,20);
	Touch_Slider.status=TOUCH_Start;
	Touch_Slider.offset=0;


}else if(Touch_Slider.status==TOUCH_Up)
{
	printf("%s	TOUCH_Up\r\n",__FUNCTION__);

	move_dir=Move_Up;
	//for(i=0;i<Touch_Slider.offset;i++)
	LCD_Vertical_Scroll_Display(move_dir,20);
	//LCD_Vertical_Scroll_Display(move_dir,Touch_Slider.offset);
	Touch_Slider.status=TOUCH_Start;
	Touch_Slider.offset=0;
}

#endif

#endif
}

void Heart_Menu_Touch()
{

	if(Touch_Slider.status==TOUCH_Down)
	{
	   Current_menu.current_menu=Sport_Menu;


	#if 0
	printf("%s  TOUCH_Down\r\n",__FUNCTION__);
		move_dir=Move_Down;
		//for(i=0;i<Touch_Slider.offset;i++)
		LCD_Vertical_Scroll_Display(move_dir,20);
		Touch_Slider.status=TOUCH_Start;
		Touch_Slider.offset=0;
		#endif

	}else if(Touch_Slider.status==TOUCH_Up)
	{

		Current_menu.current_menu=Time_Menu;
	#if 0
		printf("%s	TOUCH_Up\r\n",__FUNCTION__);

		move_dir=Move_Up;
		//for(i=0;i<Touch_Slider.offset;i++)
		LCD_Vertical_Scroll_Display(move_dir,20);
		//LCD_Vertical_Scroll_Display(move_dir,Touch_Slider.offset);
		Touch_Slider.status=TOUCH_Start;
		Touch_Slider.offset=0;

	#endif
	}







	//printf("%s\r\n",__FUNCTION__);
}
void Sport_Menu_Touch()
{

	if(Touch_Slider.status==TOUCH_Down)
	{
	   Current_menu.current_menu=Time_Menu;


#if 0
	printf("%s  TOUCH_Down\r\n",__FUNCTION__);
		move_dir=Move_Down;
		//for(i=0;i<Touch_Slider.offset;i++)
		LCD_Vertical_Scroll_Display(move_dir,20);
		Touch_Slider.status=TOUCH_Start;
		Touch_Slider.offset=0;
	#endif

	}else if(Touch_Slider.status==TOUCH_Up)
	{

		Current_menu.current_menu=Heart_Menu;
#if 0
		printf("%s	TOUCH_Up\r\n",__FUNCTION__);

		move_dir=Move_Up;
		//for(i=0;i<Touch_Slider.offset;i++)
		LCD_Vertical_Scroll_Display(move_dir,20);
		//LCD_Vertical_Scroll_Display(move_dir,Touch_Slider.offset);
		Touch_Slider.status=TOUCH_Start;
		Touch_Slider.offset=0;

#endif
	}









	printf("%s\r\n",__FUNCTION__);
}
void Setting_Menu_Touch()
{
	printf("%s\r\n",__FUNCTION__);
}
void Power_Menu_Touch()
{
	printf("%s\r\n",__FUNCTION__);
}





